<?php
	
	require_once('../includes/config.php');
	require_once('../includes/functions.php');
	
	// Retrieve base HTML
	$pageStr = buildStandardPage('Create an Account', 'Create Account');
	$content = '';
	
	if(isMobile()){
		$content .= 'Detected Mobile!';
	}
	else{
		if(false == isset($_POST['submit'])){
			if(true == isLoggedIn()){
				if(true == isCustomer() || true == isBankManager()){
					if(true == isActive()){
						$id = getID();
						
						$content .= makeAccountNavBar($id);
						
						// Create the form we'll use to capture information from the user
						$content .= '<form name=addAccountForm method=post action='. $_SERVER['PHP_SELF'] . ' >'
								 . '<table border=1 cellpadding=2 width=650>';
						  
						$content .= '<tr><td>Type</td><td><select name=type>'
								   .'<option value="Savings">Savings</option>'
								   .'<option value="Checking">Checking</option>'
								   /* .'<option value="Other">Other</option></select></td></tr>' */
						  
								   .'</table><input type=hidden name=userid value='. $_GET[$SELECTED_USER_ID] . ' >'
								   .'<input type=submit name=submit value=Submit>'
								   .'<input type=button name=cancel value=Cancel onclick="window.location=\''
								   . $ACCOUNTS_URL .'\'" >'
								   .'<input type=reset value=Reset></form>';
					}
					else{ // The current user is not active!
						$content .= $NOT_ACTIVE_MESSAGE;
					}
				} 
				else{ // The current user is not a customer or bank manager 
					$content .= $MUST_BE_BANK_MNGR;
				}
			} 
			else{ // The current user is not logged in
				$content .= $MUST_BE_LOGGED_IN;
			}
		  }
		  else{ // The user pressed the submit button
			  $_POST = cleanPost($_POST);
			  
			  $isAccountActive = false;
			  $accountBalance = 0;
			  
 			  // The account is active if the bank manager adds this to the system, 
			  // and we'll assume that the customer deposited the mandatory minimum balance.
			  if(true == isBankManager()){
				  $isAccountActive = true;
				  $accountBalance = 25;
			  }
			  
			  $query = buildInsertStatement('account', array('userid' => $_POST['userid'], 
														 'type' => $_POST['type'],
														 'minimum'=>'25',
														 'balance' =>$accountBalance,
														 'active' => $isAccountActive));

			  $result = executeSQL($query, 'Failed to insert into the database.');
			  
			  if(true == $isAccountActive){
				  	date_default_timezone_set('America/Chicago');
					$maxID = getMaxID('account');
				  
				  $insertTransactionQuery = buildInsertStatement('transaction', array('accountid' => $maxID,
																					  'type' => 'Deposit',
																					  'amount'=>'25.00',
																					  'balance'=>'25.00',
																					  'date'=> date("Y-m-d")));
				  
				  $result = executeSQL($insertTransactionQuery, 'Failed to insert into the database.');
			  }

			  // After a successful database query we'll 
			  // redirect the user back to the general accounts page.
			  header('Location: ' . $ACCOUNTS_URL);
		  }  
	}

	$pageStr = str_Replace('<!--content-->', $content, $pageStr);
	
	/**
	 * Send the generated HTML to the client's browser
	 */
	echo $pageStr;
?>